package com.pap.product.publish.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pap.base.controller.BaseController;
import com.pap.base.exception.IdValidationException;
import com.pap.base.util.jackson.JacksonUtilss;
import com.pap.obj.vo.response.ResponseVO;
import com.pap.product.agent.publish.IProductPublishAgent;
import com.pap.product.agent.publish.IProductPublishDetailAreaAgent;
import com.pap.product.agent.publish.IProductPublishDetailCarAgent;
import com.pap.product.agent.publish.dto.ProductPublishWithDetailDTO;
import com.pap.product.product.auto.entity.Product;
import com.pap.product.publish.auto.entity.ProductPublish;
import com.pap.product.publish.auto.entity.ProductPublishDetailArea;
import com.pap.product.publish.auto.entity.ProductPublishDetailCar;

@RestController
@RequestMapping("/v1/product/publish")
public class ProductPublishController extends BaseController<ProductPublish, IProductPublishAgent> {

	private static Logger logger  =  LoggerFactory.getLogger(ProductPublishController.class);
	
	@Resource(name = "productPublishAgent")
	@Override
	public void setService(IProductPublishAgent agent) {
		super.service = agent;
	}
	
	@Autowired
	private IProductPublishDetailAreaAgent productPublishDetailAreaAgent;
	
	@Autowired
	private IProductPublishDetailCarAgent productPublishDetailCarAgent;
	
	/**
	 * 数据添加
	 * @param clientLicenseId
	 * @param loginUserId
	 * @param addJSON
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveorupdate")
	public ResponseVO<ProductPublishWithDetailDTO> saveOrUpdate(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "operJSON", required = true) String operJSON) throws Exception {

		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		ProductPublishWithDetailDTO e = objectMapper.readValue(operJSON, ProductPublishWithDetailDTO.class);

		int operationInt = service.saveOrUpdateWithDetails(e, clientLicenseId, loginUserId);
		return ResponseVO.successdata(operationInt);
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/selectbyproductid/{productId}")
	public ResponseVO selectByProductId(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@PathVariable String productId) throws Exception {

		Map<Object, Object> searchMap = new HashMap<Object, Object>();
		searchMap.put("productId", productId);
		List<ProductPublishDetailArea> areaList = productPublishDetailAreaAgent.selectListByMap(searchMap);
		List<ProductPublishDetailCar> carList = productPublishDetailCarAgent.selectListByMap(searchMap);
		
		Map<String, List> returnMap = new HashMap<String, List>();
		returnMap.put("area", areaList);
		returnMap.put("car", carList);
		return ResponseVO.successdata(returnMap);
	}
	
	/**
	 * 删除操作
	 * @param clientLicenseId
	 * @param loginUserId
	 * @param deleteJSON
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/deleteall")
	public ResponseVO deleteAll(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "deleteJSON", required = true) String deleteJSON) throws Exception {

		ObjectMapper objectMapper = JacksonUtilss.getObjectMapper();
		ProductPublish e = objectMapper.readValue(deleteJSON, ProductPublish.class);
		
		if (null == e.getId()) {
			// id不存在
			throw new IdValidationException("删除操作，不存在此项数据!");
		}
		int operationInt = service.deleteWithDetails(e.getId());
		return ResponseVO.successdata(operationInt);
	}
	
	@RequestMapping(value = "/selectproductbyareaandcar")
	public ResponseVO<Product> selectProductByAreaAndCar(@RequestHeader(value = "clientLicenseId", defaultValue = "-1") String clientLicenseId,
			@RequestHeader(value = "loginUserId", defaultValue = "-1") String loginUserId,
			@RequestParam(value = "areaIds", required = true) String areaIds,
			@RequestParam(value = "carIds", required = true) String carIds) throws Exception {

		List<Product> productList = service.selectProductByAreaAndCar(areaIds, carIds);
		return ResponseVO.successdatas(productList, null);
	}
}
